const { pages } = require('./pages.config')
const { PrerenderPlugin, JpressPlugin } = require('./utils')
const path = require('path')
const resolve = p => {
  return path.resolve(__dirname, p)
}
const isDev = process.env.NODE_ENV === 'development'

module.exports = {
  pages,
  productionSourceMap: isDev,
  chainWebpack: config => {
    config.resolve.alias
      .set('@', resolve('src'))

    Object.entries(pages).forEach(page => {
      // 删除插件
      config.plugins.delete(`preload-${page[0]}`)
      config.plugins.delete(`prefetch-${page[0]}`)
      // 设置js name
      config.output.filename('js/[name].js')
      config.output.chunkFilename(`js/[name].js`)

      config
        .plugin(`html-${page[0]}`)
        .tap(args => {
          args[0].inject = false
          args[0].minify = false
          return args
        })
    })
  },
  configureWebpack: {
    // externals: isDev ? {} : {
    //   vue: 'Vue'
    // },
    plugins: isDev ? [] : [
      PrerenderPlugin(Object.entries(pages).map(p => `/${p[0]}.html`)),
      new JpressPlugin()
    ]
  },
  css: {
    extract: true
  }
}
